/**
* @depends jquery.ui.js
*/

(function($) {

	$.widget( 'ui.morify', {
		
		_init: function() {
			
			var $this = this;
			this.element.parents().each( function() {
				var o = $(this);
				if( o.css( 'display' ) == 'none' ) {
					o.addClass( 'TmpHidden' );
					o.css( 'display', 'block' );
				}
			} );
			
			var message = this.message = this.element.html();
			
			while( this.element.height() > this.options.maxHeight ) {
				message = message.replace( /(<[A-Za-z]+[^>]*>|.)$/, '' );
				this.element.html( message );
			}
			
			if( this.message != message ) {
				var a = $('<a class="More">See More</a>');
				this.element.append( a );
				a.click( function() {
					$this.element.html( $this.message );
				} );
			}
			
			this.element.parents( '.TmpHidden' ).each( function() {
				var o = $(this);
				o.removeClass( 'TmpHidden' );
				o.css( 'display', null );
			} );
		},
		
		options: {
			maxHeight: 80
		}
		
	} );
	
})(jQuery);
